<?php

class Webset extends Admin_Controller
{

    function __construct()
    {
        parent::__construct();
        $this->load->library('form_validation');
        $this->load->model('areas_m');
        $this->load->model('setting_m');
    }

    public function delcaches()
    {
        $data['title']    = 'Settings';
        $data['siderbar'] = 'webset/delcaches';
        $data['submenu']  = 'webset/delcaches';
        $this->load->driver('cache', array('adapter' => 'apc', 'backup' => 'file'));
        $this->cache->clean();
        show_message('清除站内缓存成功', site_url(X_URL . 'home'), 1);
    }

    public function allset()
    {
        if ($_POST) {
            $config['site_name']        = $this->input->post('site_name');
            $config['site_user']        = $this->input->post('site_user');
            $config['site_user_tel']    = $this->input->post('site_user_tel');
            $config['site_address']     = $this->input->post('site_address');
            $config['site_mail']        = $this->input->post('site_mail');
            $config['site_keywords']    = $this->input->post('site_keywords');
            $config['site_description'] = $this->input->post('site_description');
            $config['sub_folder']       = $this->input->post('sub_folder');
            $config['site_beian']       = $this->input->post('site_beian');
            $config['site_infos']       = $this->input->post('site_infos');
            $config['site_tel']         = $this->input->post('site_tel');
            $config['guwen_tel']        = $this->input->post('guwen_tel');
            $config['baojia_tel']       = $this->input->post('baojia_tel');
            $config['xsd_tel']          = $this->input->post('xsd_tel');
            $config['caiwu_tel']        = $this->input->post('caiwu_tel');
            $config['tousu_tel']        = $this->input->post('tousu_tel');

            $config['loan_three'] = $this->input->post('loan_three');
            $config['loan_six']   = $this->input->post('loan_six');
            $config['loan_nine']  = $this->input->post('loan_nine');
            $config['loan_lixi']  = $this->input->post('loan_lixi');

            foreach ($config as $k => $v) {
                if(empty($v)){
                    $v = '';
                }
                if ($this->setting_m->is_exist_key($k)) {
                    $this->setting_m->sava_setting($k, array('v' => $v));
                } else {
                    $this->setting_m->add_setting(array('k' => $k, 'v' => $v, 'type' => 1, "name" => ''));
                }

            }
            $this->config->set_item('webset', $config);
            $this->config->save('webset', $config);

            show_message('网站配置更新成功', site_url(X_URL . 'webset/allset'), 1);
        }

        $this->load->view('webset/allset');
    }

    public function userset()
    {
        $this->config->load('userset');//用户积分
        if ($_POST) {
            $config['credit_start'] = $this->input->post('credit_start');
            $config['credit_login'] = $this->input->post('credit_login');
            $config['kaipiao_tax']  = $this->input->post('kaipiao_tax');
            $config['service_tax']  = $this->input->post('service_tax');

            foreach ($config as $k => $v) {
                if(empty($v)){
                    $v = '';
                }
                if ($this->setting_m->is_exist_key($k)) {
                    $this->setting_m->sava_setting($k, array('v' => $v));
                } else {
                    $this->setting_m->add_setting(array('k' => $k, 'v' => $v, 'type' => 1, "name" => ''));
                }

            }

            $this->config->set_item('userset', $config);
            $this->config->save('userset', $config);
            show_message('税点配置更新成功', site_url(X_URL . 'webset/userset'), 1);
        }

        $this->load->view('webset/userset');
    }

    public function categoryset()
    {
        if ($_POST) {
            $config['web_category_txt'] = $this->input->post('web_category_txt');
            $config['web_category']     = $this->input->post('web_category');
            $this->config->set_item('categoryset', $config);
            $this->config->save('categoryset', $config);
            show_message('频道配置更新成功', site_url(X_URL . 'webset/categoryset'), 1);
        }

        $this->load->view('webset/categoryset');
    }

    public function webapiset()
    {
        $this->config->load('webapiset');
        $data = array();
        if ($this->config->item('tengxun_ip_ak')) {
            $data['tengxun_info']  = $this->myclass->user_baidu($this->config->item('tengxun_ip_ak'), 'city');
            $data['zhifubao_info'] = $this->myclass->user_baidu($this->config->item('zhifubao_ak'), 'city');
            $data['weixin_info']   = $this->myclass->user_baidu($this->config->item('weixin_ak'), 'city');
        }
        if ($_POST) {
            $config['tengxun_ip_ak'] = $this->input->post('tengxun_ip_ak');
            $config['zhifubao_ak']   = $this->input->post('zhifubao_ak');
            $config['weixin_ak']     = $this->input->post('weixin_ak');
            $this->config->set_item('webapiset', $config);
            $this->config->save('webapiset', $config);
            show_message('API配置更新成功', site_url(X_URL . 'webset/webapiset'), 1);
        }

        $this->load->view('webset/webapiset');
    }

    public function picset()
    {
        $this->config->load('picset');
        if ($_POST) {
            $config['ad_pic_set'] = $this->input->post('ad_pic_set');
            $this->config->set_item('picset', $config);
            $this->config->save('picset', $config);

            show_message('图片配置更新成功', site_url(X_URL . 'webset/picset'), 1);
        }

        $this->load->view('webset/picset');
    }

    public function city_json()
    {
        $province = $this->areas_m->get_sons_by_pid();
        $data     = array();
        foreach ($province as $k => $v) {
            $data[$k]['id']   = $v['area_id'];
            $data[$k]['name'] = $v['area_name'];
            $a                = $this->areas_m->get_sons_by_pid($v['area_id']);
            foreach ($a as $ka => $va) {
                $data[$k]['child'][$ka]['id']   = $va['area_id'];
                $data[$k]['child'][$ka]['name'] = $va['area_name'];
                $b                              = $this->areas_m->get_sons_by_pid($va['area_id']);
                if ($b) {
                    foreach ($b as $kb => $vb) {
                        $data[$k]['child'][$ka]['child'][$kb]['id']   = $vb['area_id'];
                        $data[$k]['child'][$ka]['child'][$kb]['name'] = $vb['area_name'];
                    }
                }

            }
        }
        $config['cityjson'] = json_encode($data, JSON_UNESCAPED_UNICODE);
        $this->config->set_item('cityjson', $config);
        $this->config->save('cityjson', $config);
        echo '1';
    }

    public function loglist($page = 1)
    {
        /** 检查登陆 */
        if ($this->auth->is_admin()) {
            $data['title']    = '登陆日志';
            $data['siderbar'] = 'webset/allset';
            $data['submenu']  = 'webset/allset';
            $data['total']    = $this->log_m->count_log();
            //分页
            $limit                      = 20;
            $config['uri_segment']      = 4;
            $config['use_page_numbers'] = true;
            $config['base_url']         = site_url(X_URL . 'webset/loglist');
            $config['total_rows']       = $data['total'];
            $config['per_page']         = $limit;
            $config['prev_link']        = '&larr;';
            $config['prev_tag_open']    = '<li class=\'prev\'>';
            $config['prev_tag_close']   = '</li';
            $config['cur_tag_open']     = '<li class=\'active\'><span>';
            $config['cur_tag_close']    = '</span></li>';
            $config['num_tag_open']     = '<li>';
            $config['num_tag_close']    = '</li>';
            $config['next_link']        = '&rarr;';
            $config['next_tag_open']    = '<li class=\'next\'>';
            $config['next_tag_close']   = '</li>';
            $config['first_link']       = 'First Page';
            $config['first_tag_open']   = '<li class=\'first\'>';
            $config['first_tag_close']  = '</li>';
            $config['last_link']        = 'Last Page';
            $config['last_tag_open']    = '<li class=\'last\'>';
            $config['last_tag_close']   = '</li>';
            $config['num_links']        = 5;

            $this->load->library('pagination');
            $this->pagination->initialize($config);

            $start              = ($page - 1) * $limit;
            $data['pagination'] = $this->pagination->create_links();

            $data['info_list'] = $this->log_m->log_list($start, $limit);
            $this->load->view('logs_list', $data);
        } else {
            show_message('您没有此管理权限', site_url('user/login'));
        }
    }

    public function dellog($id)
    {
        /** 检查登陆 */
        if ($this->auth->is_admin()) {
            $data['title']    = '删除日志';
            $data['siderbar'] = 'webset/allset';
            $data['submenu']  = 'webset/allset';
            //删除
            if ($this->log_m->del_log($id)) {
                show_message('删除日志成功', site_url(X_URL . 'webset/loglist'), 1);
            }

        } else {
            show_message('您没有此管理权限', site_url('user/login'));
        }
    }

    public function dellog_all()
    {
        /** 检查登陆 */
        if ($this->auth->is_admin()) {
            $data['title']    = '删除日志';
            $data['siderbar'] = 'webset/allset';
            $data['submenu']  = 'webset/allset';
            //删除
            $this->log_m->del_all();
            show_message('清空日志成功', site_url(X_URL . 'webset/loglist'), 1);

        } else {
            show_message('您没有此管理权限', site_url('user/login'));
        }
    }


}